<template>
	<view class="content">
		<view class="uni-padding-wrap">
			<view class="uni-title uni-common-mt">
				{{job_detail.displayName}}<br />
				<text>描述: {{job_detail.description}}</text><br />
				<text>是否禁用: {{job_detail.disabled}}</text><br />
				<text>可发布: {{job_detail.buildable}}</text><br />
				<text>队列执行中: {{job_detail.inQueue}}</text><br />
			</view>
			<view class="uni-common-mt" style="background:#FFF; padding:20rpx;">
				<button v-if="job_detail.buildable && !build && !job_detail.inQueue" @click="buildJob">Build Now</button>
				<button type="warn" v-if="build && current_build_number != null" @click="stopBuildJob">Stop Build {{current_build_number}}</button>
			</view>
		</view>
		<view class="uni-padding-wrap">
			<view class="uni-title">
				History (Recent 10)
				<template v-for="(item,index) in history_builds" v-if="index < 10">
					<view class="uni-list" style="font-size: 12rpx; padding-left: 20rpx; color: #8F8F94; line-height: 60rpx;" @click="getHistoryDetail(index, '正在查询构建详情')">
						Build {{item.number}}  
						<template v-if="item.build_info != null">
							<template v-if="item.build_info.building">
								<uni-tag style="display: inline;" text=" 正在构建... " type="error" :circle="true"></uni-tag>
							</template>
							<template v-else>
								   {{'      '+item.build_info.time + ' ' +item.build_info.result}}
							</template>
						</template>
					</view>
				</template>
			</view>
		</view>
	</view>
</template>

<script>
	import uniTag from "@/components/uni-tag/uni-tag.vue"
	export default {
		components: {
			uniTag
		},
		data() {
			return {
				jenkins_config_id: "",
				job_name: "",
				token: null,
				job_detail: {
					displayName: "",
					description: "",
					buildable: false,
					inQueue: false
				},
				buildable: false,
				history_builds: [],
				next_build_number: null,
				build:false,
				current_build_number: null
			}
		},
		onLoad() {
			this.jenkins_config_id = this.$route.query.jenkins_config_id
			this.job_name = this.$route.query.job_name
		},
		onShow() {
			if (this.token == null) {
				var _this = this
				var token_obj = this.common.checkNeedLogin()
				if (token_obj != null) {
					this.token = token_obj.token
				}
				this.getJobDetail()
			}
		},
		methods: {
			getJobDetail() {
				var _this = this
				this.common.requestAPI(this.common.config.API_GET_JOB_DETAIL, 'GET', {
					token: this.token,
					jenkins_config_id: this.jenkins_config_id,
					job_name: this.job_name
				}, '正在拉取Job信息', function(res) {
					_this.job_detail = res.result
					_this.buildable = res.result.buildable
					_this.history_builds = res.result.builds
					_this.next_build_number = res.result.nextBuildNumber
					_this.getHistoryDetail(0, null)
				})
			},
			buildJob() {
				var _this = this
				this.common.requestAPI(this.common.config.API_BUILD_JOB, 'POST', {
					token: this.token,
					jenkins_config_id: this.jenkins_config_id,
					job_name: this.job_name
				}, '正在发起构建', function(res) {
					uni.showToast({
						title: "发起成功"
					})
					_this.build = true
					_this.current_build_number = _this.next_build_number
					_this.history_builds.unshift({number:_this.next_build_number})
				})
			},
			stopBuildJob() {
				var _this = this
				this.common.requestAPI(this.common.config.API_STOP_JOB, 'POST', {
					token: this.token,
					jenkins_config_id: this.jenkins_config_id,
					job_name:_this.job_name,
					history_number: this.current_build_number
				}, '正在尝试停止构建', function(res) {
					uni.showToast({
						title: "操作成功"
					})
					_this.build = false
					_this.current_build_number = null
					_this.getJobDetail()
				})
			},
			getHistoryDetail(index, loading_text = null) {
				var _this = this
				this.common.requestAPI(this.common.config.API_GET_JOB_HISTORY, 'GET', {
					token: this.token,
					jenkins_config_id: this.jenkins_config_id,
					job_name: this.job_name,
					history_number: this.history_builds[index].number
				}, loading_text, function(res) {
					_this.$set(_this.history_builds, index, {
						build_info: {
							time: _this.common.formatDate(res.result.timestamp),
							result: res.result.result,
							building: res.result.building
						},
						number: _this.history_builds[index].number
					})
					if (res.result.building) {
						_this.build = true
						_this.current_build_number = _this.history_builds[index].number
					}
					if (index == 0 && !res.result.build) {
						_this.build = false
						_this.current_build_number = null
					}
				})
			}
		}
	}
</script>

<style>

</style>
